#include "EnumValueBuilder.h"
#include "ScriptCompiler/AST/EnumValueNode.h"
#include "ScriptCompiler/ASTBuilder/ExpressionBuilder.h"    
#include "ScriptCompiler/ScriptFileContext.h"

CEnumValueBuilder::CEnumValueBuilder(
    std::shared_ptr<CScriptFileContext> InContext
    ,int64_t InEndTokenIndex
    )
{
    Context=InContext;
    EndTokenIndex=InEndTokenIndex;
    Result=std::make_shared<CEnumValueNode>(Context);
}

bool CEnumValueBuilder::ReadCurentToken()
{
    //build name
    {
        auto Token=Context->GetCurrentToken();
        if (Token->Type==EScriptTokenType::Identifier)
        {
            Result->Name=Token;
            Context->CurrentTokenIndex++;
        }
        else
        {
            Context->RaiseError(10180,Token,U"Expected enum value name");
            return false;
        }
    }

    //try find =
    {
        auto Token=Context->GetCurrentToken();
        if (Token->Type!=EScriptTokenType::Assign)
        {
            if(Context->CurrentTokenIndex!=EndTokenIndex)
            {
                Context->RaiseError(10181,Token,U"unexpected token in enum value");
                return false;
            }

            //do not have value
            return true;
        }
        else
        {
            Context->CurrentTokenIndex++; //skip =
        }
    }

    //build value
    {
        auto ExpressionBuilder=std::make_shared<CExpressionBuilder>(Context,EndTokenIndex);
        if(!ExpressionBuilder->ReadCurentToken())
        {
            return false;
        }
        Result->Value=ExpressionBuilder->Result;
    }

    return true;
}
